import {Component, Input, OnInit, ViewEncapsulation} from '@angular/core';
import {MatchService} from "app/pages/match/match.service";
import {Router} from '@angular/router';

@Component({
  selector: 'app-match-card',
  encapsulation: ViewEncapsulation.None,
  templateUrl: './match-card.component.html',
  styleUrls: ['./match-card.component.scss']
})
export class MatchCardComponent implements OnInit {
  @Input() matchs; // 球队历史赛程
  @Input() gradeArr; // 淘汰赛最后三轮  4/1决赛  半决赛  决赛

  public scheduleType: number;
  public pageDate = {
    joinRequireType: 0, // 0:个人，1:双人
    isCup: false // 是否杯赛
  };

  constructor(
    public service: MatchService,
    public router: Router
  ) {
    this.scheduleType = this.service.competitionStore.scheduleType;
    this.service.competitionStore.cupCodeArr.filter((value) => {
      if (this.scheduleType === value) {
        this.pageDate.isCup = true;
      }
    });
  }

  /**
   * 跳转查看小比分
   */
  public jumpTo(item) {
    // if (this.service.competitionStore.matchType !== 3001 && this.service.competitionStore.matchType !== 3002) {
    //   return;
    // } else if (item.status !== 6 && item.status !== 4 && item.status !== 1) {
    //   this.router.navigate(['/competitions/match-score'], {
    //     queryParams: {
    //       matchScheduleId: item.id,
    //       isTeam: this.service.competitionStore.competitionType,
    //       joinRequire: this.service.competitionStore.joinRequire
    //     }
    //   });
    // }

    const queryParams = {
      matchScheduleId: item.id,
      isTeam: this.service.competitionStore.competitionType,
      joinRequire: this.service.competitionStore.joinRequire,
      matchType: this.service.competitionStore.matchType,
      scheduleType: this.scheduleType,
      hostUserName2: '',
      guestUserName2: ''
    };

    if (this.pageDate.isCup && this.pageDate.joinRequireType === 1) {
      queryParams.hostUserName2 = encodeURI(item.hostUserName2);
      queryParams.guestUserName2 = encodeURI(item.guestUserName2);
    } else {
      delete queryParams.hostUserName2;
      delete queryParams.guestUserName2;
    }

    if (this.service.competitionStore.behalfSmallScore === 1) {
      if (item.status !== 6 && item.status !== 4 && item.status !== 1) {
        this.router.navigate(['/match/score'], {
          queryParams: queryParams
        });
      }
    }
  }

  /**
   *检查参赛要求
   */
  public checkJoinRequire(code) {
    this.service.competitionStore.joinRequireArr.filter((value) => {
      if (code === value) {
        this.pageDate.joinRequireType = 1;
      }
    });
  }

  ngOnInit() {
    this.checkJoinRequire(this.service.competitionStore.joinRequire);
  }
}
